// 初始游戏画面等操作
// 收集所有的计时器，作为游戏暂停，结束控制
let timers = []
// 判断游戏赢了
let dieTimer = []
// 设置僵尸数量，作为判断游戏进度，提示游戏赢了
let zoms = 8
let conut = zoms
// 僵尸固定的几个y坐标
let zomLocationY = [0,100,200,300,400]
//记录僵尸生成的Y坐标，保证每一条路都有僵尸
let collectZomY = []
let remianY = []

new PlantsCard({x:0,y:0,id:0,type:1,path:'plantsicon/1_0000s_0023_28.png'}).draw()
new PlantsCard({x:100,y:0,id:1,type:2,path:'plantsicon/jiqiangsheshou_5_001.png'}).draw()
new PlantsCard({x:200,y:0,id:2,type:3,path:'/nature_fire.png'}).draw()

// 简单初始化几个僵尸
let oldTime = new Date().getTime()
let nowTime = null
let y = 50
let index = 0
let createZombies = setInterval(()=>{
    if(conut<=0) clearInterval(createZombies)
    y = zomLocationY[Math.ceil(Math.random()*3)]
    collectZomY.push(y)
    nowTime = new Date().getTime()
    if(nowTime-oldTime>=2000){
        if(conut/zoms<=2/3){
            if(remianY.length>0) {
                mark = false
                index = Math.ceil(Math.random()*(remianY.length-1))
                y = remianY[index]
                remianY.splice(index,1)
            }else{
                collectZomY = [...new Set(collectZomY)]
                zomLocationY.forEach(item=>{
                    if(!collectZomY.includes(item)){
                        remianY.push(item)
                    }
                })
            }
        }
        oldTime = new Date().getTime()
        new Zombies({x:700,y:y,id:conut,type:0,name:'idle_00.png'}).draw()
        conut -= 1
    }
},1000)
let winGame = setInterval(()=>{
    if(dieTimer.length>=zoms){
        console.log(timers.length)
        // 暂停所有计时器
        timers.forEach(item=>{
            clearInterval(item)
        })
        alert('恭喜你赢了')
        clearInterval(winGame)
    }
},1000)